<?php
/**
 * Description of QnContentclass
 *
 * @author Chisheng Lu, Jeffy shih <jeffy@dorise.info>
 * @copyright Copyright(c)2010 DoRISE team, Science Education center, NTNU.
 * @since 2010-05-13
 * @version 1.3
 */
class QnContent {
    //put your code here
    private $mysqli;

    function __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    /**
     *
     * @param Integer $intQnId Questionnaire Id
     * @param Array $aryQnIId
     * @return Boolean
     */
    public function addQnContent($intQnId, $aryQnIId) {
        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_addQC = 'INSERT INTO `QnContent` (`QnId`, `QnIId`)';
        $sql_values = ' VALUES';

        $bolFirst = true;
        foreach ($aryQnIId as $intQnIId) {
            if($bolFirst) {
                $sql_values .= '(' . $intQnId . ', ' . $intQnIId . ')';
                $bolFirst = false;
            }else {
                $sql_values .= ', (' . $intQnId . ', ' . $intQnIId . ')';
            }
        }

        $sql_addQC .= $sql_values;

        $rs_addQC = $this->mysqli->query($sql_addQC);
        if (!$rs_addQC) {
            throw new Exception('新增問卷內容時發生錯誤！');
        }else {
            $bolRtn = true;
        }
        return $bolRtn;

    }

    /**
     *
     * @param Integer $intQnCId
     * @param Array $aryQnContent
     * @return Boolean
     */
    public function editQnContent($intQnCId, $aryQnContent) {
        //檢查問卷內容編號是否為空白
        if (empty ($intQnCId)) {
            throw new Exception('缺少問卷內容編號！');
        }
        $bolRtn = false;

        $sql_editQC = 'Update QnContent set ';
        if (array_key_exists('QnId', $aryQnContent))
            $sql_editQC .= 'QnId = ' . $aryQnContent['QnId'];
        if (array_key_exists('QnIId', $aryQnContent))
            $sql_editQC .= ', QnIId = ' . $aryQnContent['QnIId'];
        $sql_editQC .= ' where QnCId = ' . $intQnCId . ';';
        //echo "sql_editQC:$sql_editQC<br>";
        $rs_editQC = $this->mysqli->query($sql_editQC);
        if (!$rs_editQC) {
            throw new Exception('更新問卷內容時發生錯誤！');
        }else {
            $bolRtn = true;
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intQnId Questionnaire Id
     * @param Array $aryQnIId One dimension array, Item Id
     * @since version 1.3
     * @return Boolean
     */
    public function editQnContentByQnId($intQnId, $aryQnIId){
        //檢查問卷內容編號是否為空白
        if (empty ($intQnId)) {
            throw new Exception('缺少問卷內容編號！');
        }
        $bolRtn = false;
        if($this->delQnContentByQnId($intQnId)){
            if($this->addQnContent($intQnId, $aryQnIId)){
                $bolRtn = true;
            }
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intQnCId
     * @return Boolean
     */
    public function delQnContent($intQnCId) {
        $bolRtn = false;
        //檢查問卷內容編號是否為空白
        if (empty ($intQnCId)) {
            throw new Exception('錯誤！缺少問卷內容編號。');
        }

        //刪除
        $sql_delQC = 'DELETE FROM `QnContent` WHERE `QnCId`=' . $intQnCId;
        $rs_delQC = $this->mysqli->query($sql_delQC);
        if (!$rs_delQC) {
            throw new Exception('錯誤！刪除問卷內容時發生錯誤。');
        }else {
            $bolRtn = true;
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intQnId
     * @return Boolean
     */
    public function delQnContentByQnId($intQnId) {
        //檢查問卷編號是否為空白
        if (empty ($intQnId)) {
            return false;
        }

        //刪除
        $sql_delQC = 'DELETE FROM `QnContent` WHERE `QnId`=' . $intQnId;
        $rs_delQC = $this->mysqli->query($sql_delQC);
        if (!$rs_delQC) {
            return false;
        }else {
            return true;
        }
    }

    /**
     *
     * @param Integer $intQnCId
     * @return Array
     */
    public function getQnContentInfoByQnCId($intQnCId) {
        //檢查問卷內容編號是否為空白
        if (empty ($intQnCId)) {
            throw new Exception('缺少問卷內容編號！');
        }

        $sql_getQCById = 'SELECT * FROM `QnContent` WHERE `QnCId`=' . $intQnCId;
        $rs_getQCById = $this->mysqli->query($sql_getQCById);
        if (!$rs_getQCById) {
            throw new Exception('無法查詢該問卷內容的資料！');
        }else {
            $row_getQCById = $rs_getQCById->fetch_row();
            return $row_getQCById;
        } //End If

    }

    /**
     *
     * @param Integer $intQnId Questionnaire Id
     * @return Array One Dimension array
     */
    public function getQnContentItemCTitleByQnId($intQnId) {
        //檢查問卷編號是否為空白
        if (empty ($intQnId)) {
            throw new Exception('缺少問卷內容編號！');
        }

        $sql_QnItemTitle = 'SELECT `QnItem`.`QnICTitle` FROM `QnContent`
            INNER JOIN `QnItem` ON `QnContent`.`QnIId` = `QnItem`.`QnIId`
            WHERE `QnContent`.`QnId`=' . $intQnId;
        $rs_QnItemTitle = $this->mysqli->query($sql_QnItemTitle);

        if (!$rs_QnItemTitle) {
            throw new Exception('無法查詢該問卷內容的資料！');
        }else {
            $aryQnItemTitle = array();
            while ($row_QnItemTitle = $rs_QnItemTitle->fetch_row()) {
                array_push($aryQnItemTitle, $row_QnItemTitle[0]);
            }
            return $aryQnItemTitle;
        } //End If
    }

    /**
     *
     * @since version 1.2
     * @param Integer $intQnId Questionnaire Id
     * @return Array Two dimension array
     */
    public function getFullQnContentItemByQnId($intQnId) {
        //檢查問卷編號是否為空白
        if (empty ($intQnId)) {
            throw new Exception('缺少問卷內容編號！');
        }

        $sql_QnItem = 'SELECT `QnContent`.`QnCId`, `QnItem`.* FROM `QnContent`
            INNER JOIN `QnItem` ON `QnContent`.`QnIId` = `QnItem`.`QnIId`
            WHERE `QnContent`.`QnId`=' . $intQnId;
        $rs_QnItem = $this->mysqli->query($sql_QnItem);

        if (!$rs_QnItem) {
            throw new Exception('無法查詢該問卷內容的資料！');
        }else {
            $aryQnItem = array();
            while ($row_QnItem = $rs_QnItem->fetch_row()) {
                array_push($aryQnItem, $row_QnItem);
            }
            return $aryQnItem;
        } //End If
    }

    /**
     *
     * @return Array
     */
    public function getQnContentList() {
        $sql_getQCList = 'SELECT * FROM `QnContent` ;';
        $rs_getQCList = $this->mysqli->query($sql_getQCList);

        if (!$rs_getQCList) {
            throw new Exception('無法查詢該問卷內容的資料！');
        }else {
            $aryQCList = array();
            while ($aryOneQC = $rs_getQCList->fetch_row()) {
                array_push($aryQCList, $aryOneQC);
            }
            return $aryQCList;
        } //End If
    }

    /**
     *
     * @param Integer $intQnId Questionnaire Id
     * @return Array One dimension array
     */
    public function getQnItemIdListByQnId($intQnId){
        //檢查問卷編號是否為空白
        if (empty ($intQnId)) {
            throw new Exception('缺少問卷內容編號！');
        }

        $aryRtn = array();
        $sql_getItemId = 'SELECT `QnIId` FROM `QnContent` WHERE `QnId`=' . $intQnId . ' ORDER BY `QnIId`';
        $rs_getItemId = $this->mysqli->query($sql_getItemId);

        if(!$rs_getItemId){
            throw new Exception('無法查詢該問卷內容的項目編號！');
        }else{
            while($row_getItemId = $rs_getItemId->fetch_row()){
                array_push($aryRtn,$row_getItemId[0]);
            }
        }

        return $aryRtn;
    }

    /**
     *
     * @return Array
     */
    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
